import { findLastIndex } from 'lodash-es'

export const genPageResponse = (records = [], total = 0) => {
  records = records || []
  return genEmptyResponse({
    records,
    total: total || records.length
  })
}

export const genEmptyResponse = (data = {}) => {
  return {
    code: 0,
    data,
    extra: null,
    message: 'success',
    path: null,
    timestamp: Date.now()
  }
}

export function getUnitInfo({ ouFullCode, ouFullName }) {
  let unitCode
  let unitName
  if (ouFullCode && ouFullName) {
    const codes = ouFullCode.split('.')
    const names = ouFullName.split('.')
    let index = findLastIndex(codes, value => value.indexOf('DW') > -1)
    if (index === -1) {
      index = 0
    }
    unitCode = codes[index]
    unitName = names[index]
  }
  return { unitCode, unitName, unitId: unitCode?.replace('DW', '') }
}
